#-*- coding:UTF-8 -*-
"""
Version: v1
Last Modified:
All rights reserved
"""
import httplib, mimetypes
import MySQLdb
import sys 
import time
import urllib2
import pickle

def encode_multipart_formdata(fields, files):
    """
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return (content_type, body) ready for httplib.HTTP instance
    """
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
    CRLF = '\r\n'
    L = []
    for (key, value) in fields:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"' % key)
        L.append('')
        L.append(value)
    for (key, filename, value) in files:
        L.append('--' + BOUNDARY)
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
        L.append('Content-Type: %s' % get_content_type(filename))
        L.append('')
        L.append(value)
    L.append('--' + BOUNDARY + '--')
    L.append('')
    body = CRLF.join(L)
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
    return content_type, body

def get_content_type(filename):
    return 'application/octet-stream'
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'



def post_multipart(host, selector, fields, files):
    """
    Post fields and files to an http host as multipart/form-data.
    fields is a sequence of (name, value) elements for regular form fields.
    files is a sequence of (name, filename, value) elements for data to be uploaded as files
    Return the server's response page.
    """
    content_type, body = encode_multipart_formdata(fields, files)
    h = httplib.HTTPConnection(host)
    h.putrequest('POST', selector)
    h.putheader('content-type', content_type)
    h.putheader('content-length', str(len(body)))
    h.endheaders()
    h.send(body)
    resp=h.getresponse()
    return resp

DB="E:/GeoLiteCity_20090401/GeoLiteCityBlocks.csv"
LOCATION="E:/GeoLiteCity_20090401/GeoLiteCityLocation.csv"

HOST="localhost:8080"
HOST="pyutility.appspot.com"


MAXINT=12343741319167

def main():
    db=open(DB,"r")
    id=0
    #skip
    skip=636
    for i in range(skip):
        id+=1
        linenum=0
        while linenum<5000:
            line=db.readline()
            linenum+=1
    
    loop=True
    while loop:
        obj=[]
        start=MAXINT
        end=0
        id+=1
        linenum=0
        while linenum<5000:
            line=db.readline()
            if line=="":
                loop=False
                break
            linenum+=1
            lines=line.split(",")
            s=int(eval(lines[0]))
            e=int(eval(lines[1]))
            l=int(eval(lines[2]))
            if start>s:
                start=s
            if end<e:
                end=e
            obj.append((s,e,l))
        
        fields=[("id",str(id))]
        fields.append(("start",str(start)))
        fields.append(("end",str(end)))
        data=pickle.dumps(obj,2)
        files=[("data","tmp",data)]
        retry=True
        while retry:
            retry=False
            try:
                resp=post_multipart(HOST,"/api/storeblock",fields,files)
                print resp.status,resp.read()
            except Exception,e:
                print e
                retry=True

def location():
    
    db=open(LOCATION,"r")
    id=0
    skip=0
    for i in range(skip):
        id+=1
        linenum=0
        while linenum<1000:
            line=db.readline()
            linenum+=1
    loop=True
    while loop:
        obj=[]
        start=MAXINT
        end=0
        id+=1
        linenum=0
        while linenum<1000:
            line=db.readline()
            if line=="":
                loop=False
                break
            linenum+=1
            lines=line.split(",")
            o={
               "loc":int(lines[0]),
               "country":lines[1].decode("ISO-8859-1"),
               "region":lines[2].decode("ISO-8859-1"),
               "city":lines[3].decode("ISO-8859-1"),
               "postal":lines[4].decode("ISO-8859-1"),
                "latitude":float(lines[5]),
                "longitude":float(lines[6]),
                "metro":lines[7].decode("ISO-8859-1"),
                "area":lines[8].decode("ISO-8859-1")
               }
            loc=int(lines[0])
            if start>loc:
                start=loc
            if end<loc:
                end=loc
            obj.append(o)
        
        fields=[("id",str(id))]
        fields.append(("start",str(start)))
        fields.append(("end",str(end)))
        data=pickle.dumps(obj,2)
        files=[("data","tmp",data)]
        retry=True
        while retry:
            retry=False
            try:
                resp=post_multipart(HOST,"/api/storeloc",fields,files)
                print resp.status,resp.read()
            except Exception,e:
                print e
                retry=True
    
if __name__=="__main__":
    location()
    #main()
